home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9698 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c,comp.unix.aix
  4. Subject: Re: Help: write() to a socket hangs unless there's a newline
  5. Date: 12 Mar 1996 12:44:46 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4i4nnuINNgc@keats.ugrad.cs.ubc.ca>
  8. References: <5r68cae4wm.fsf@ritz.mordor.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <5r68cae4wm.fsf@ritz.mordor.com>,
  12. Joseph Thomas <benjamin@ritz.mordor.com> wrote:
  13.  >I realize this is not a direct C question, but it does have to do with
  14.  >streams, and I tend to think the expertise to answer this is here:
  15.  >
  16.  >I've opened a socket, and connected it to another.  I'm using write() to
  17.  >send a message through, ex:
  18.  >
  19.  >write(sock_fd, "Hello world", strlen("Hello world"));
  20.  >
  21.  >When I do this, the message gets through to the other socket, but the
  22.  >write() call hangs in the process that executed it.
  23.  
  24. The message gets through, but what comes out at the other end is not a string.
  25. You forgot to include the zero terminating character by adding 1 to strlen().
  26.  
  27. Then again, you never specified what kind of socket this is---i.e. whether it
  28. is a stream-oriented virtual circuit or a datagram socket. In a datagram
  29. socket, the reader can append its own zero characters to strings, by, for
  30. instance, reading the data into a large zero-filled buffer. Over a stream
  31. socket, however, if you do the above, you have no easy means of delimiting
  32. where one string starts and another one ends if you don't include a marker such
  33. as the null terminator.
  34.  
  35.  >However, if I send it as: 
  36.  >
  37.  >write(sock_fd, "Hello world\n", strlen("Hello world\n"));
  38.  >
  39.  >It doesn't hang, but I have a newline I don't want at the other end.
  40.  
  41. Sounds weird. That should not happen. Who knows what is going on? You didn't
  42. post the rest of your program.
  43.  
  44.  >In addition, for some reason, between the next write()-->read() that I
  45.  >try, I read() two null strings before I actually get the next
  46.  >message (this may be something particular to sockets?)
  47.  >
  48.  >I've tried using fflush(sock_fd), but that causes a broken pipe for
  49.  >some reason.  Can anyone say what gives here?
  50.  
  51. You are confused. fflush() is a standard C library function that works
  52. with FILE * streams, not integer file descriptors.
  53.  
  54.  >Please, no advice about not using write()
  55.  
  56. How about, take this to comp.unix.programmer? One is not required to know about
  57. socket programming in order to be a C guru. Or ask on Internet Relay Chat in
  58. the #unix channel.
  59. -- 
  60.  
  61.